package com.woniu.springbootshiro.controller;

import com.woniu.springbootshiro.pojo.AlarmLight;

import com.woniu.springbootshiro.service.AlarmLightService;
import com.woniu.springbootshiro.service.LampInfoService;
import com.woniu.springbootshiro.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AlarmLightController {
    @Autowired
    private AlarmLightService als;

    @Autowired
    private LampInfoService lis;

    @RequestMapping("/queryAllAlarmLight")
    @ResponseBody
    public List<AlarmLight> queryAllAlarmLight(){
        return als.queryAllAlarmLight();
    }

    @GetMapping("/exportTest")
    @ResponseBody
    public void test(HttpServletResponse response) throws IOException {
        List<AlarmLight> list=new ArrayList<>();
        list=als.queryAllAlarmLight();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition","attachment;filename="+"test.xlsx");
        ExcelUtil.writeExcel(response,list);
    }


    @RequestMapping("/addAlarmLight")
    @ResponseBody
    public int addAlarmLight(AlarmLight alarmLight){
        als.addAlarmLight(alarmLight);
        lis.updateAlarmLight(alarmLight.getId());
        return 1;
    }

    @RequestMapping("/dealAlarmLight")
    @ResponseBody
    public int dealAlarmLight(Integer id){
        als.updateAlarm(id);
        lis.dealLampInfo(id);
        return 1;
    }

}
